package ru.galiauir.gui;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class ResultForm {
	private static final int TOP_MARGIN = 10;
	private static final int LEFT_MARGIN = 10;
	private static final int FIELD_HEIGHT = 30;
	private static final int LABEL_WIDTH = 200;
	private static final int FIELD_WIDTH = 100;
	private static final int FIELD_STEP = FIELD_HEIGHT + 10;
	private static final int FIELD_MARGIN_LEFT = 5;
	
	private static final int FRAMEWIDTH = LEFT_MARGIN + LABEL_WIDTH + FIELD_WIDTH + LEFT_MARGIN;
	private static final int FRAMEHEIGHT = TOP_MARGIN + 4 * FIELD_STEP;
		
	private JFrame 		jfrmProtocols;
	private Container 	cp;
	
	private JLabel jlblAverage;
	private JTextField jtxtAverage;
	
	private JLabel jlblBonusGiven;
	private JTextField jtxtBonusGiven;
	
	private JLabel jlblBonusSpent;
	private JTextField jtxtBonusSpent;
	
	public ResultForm(long average, long given, long spent) {
		jfrmProtocols = new JFrame("Настройки модели");
		jfrmProtocols.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		jfrmProtocols.setSize(FRAMEWIDTH,FRAMEHEIGHT);
		jfrmProtocols.setResizable(false);	
		setFrameLocation(jfrmProtocols);
		jfrmProtocols.addWindowListener(new WindowAdapter(){});
		
		createObjects(average, given, spent);				
		initObjects();
		addObjects();
		
		jfrmProtocols.setVisible(true);
	}
	
	private void setFrameLocation(JFrame frm) {
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		int left = ((int) (dim.getWidth() - frm.getWidth()) / 2);
		int top = ((int) (dim.getHeight() - frm.getHeight()) / 3);
		frm.setLocation(left, top);
	}
	
	private void createObjects(long average, long given, long spent) {
		jlblAverage = new JLabel("Средняя сумма покупки");
		jtxtAverage = new JTextField(String.valueOf(average));	

		jlblBonusGiven = new JLabel("Начислено бонусов");
		jtxtBonusGiven = new JTextField(String.valueOf(given));	

		jlblBonusSpent = new JLabel("Потрачено бонусов");
		jtxtBonusSpent = new JTextField(String.valueOf(spent));		
	}
	
	private void initObjects() {
		
		jlblAverage.setBounds(LEFT_MARGIN, TOP_MARGIN, LABEL_WIDTH, FIELD_HEIGHT);
		jlblAverage.setVisible(true);
		
		jtxtAverage.setBounds(LEFT_MARGIN + LABEL_WIDTH + FIELD_MARGIN_LEFT, TOP_MARGIN, FIELD_WIDTH, FIELD_HEIGHT);
		jtxtAverage.setVisible(true);
		jtxtAverage.setEditable(false);
		
		jlblBonusGiven.setBounds(LEFT_MARGIN, TOP_MARGIN + 1 * FIELD_STEP, LABEL_WIDTH, FIELD_HEIGHT);
		jlblBonusGiven.setVisible(true);
		
		jtxtBonusGiven.setBounds(LEFT_MARGIN + LABEL_WIDTH + FIELD_MARGIN_LEFT, TOP_MARGIN + 1 * FIELD_STEP, FIELD_WIDTH, FIELD_HEIGHT);
		jtxtBonusGiven.setVisible(true);
		jtxtBonusGiven.setEditable(false);
		
		jlblBonusSpent.setBounds(LEFT_MARGIN, TOP_MARGIN + 2 * FIELD_STEP, LABEL_WIDTH, FIELD_HEIGHT);
		jlblBonusSpent.setVisible(true);
		
		jtxtBonusSpent.setBounds(LEFT_MARGIN + LABEL_WIDTH + FIELD_MARGIN_LEFT, TOP_MARGIN + 2 * FIELD_STEP, FIELD_WIDTH, FIELD_HEIGHT);
		jtxtBonusSpent.setVisible(true);
		jtxtBonusSpent.setEditable(false);
	}
	
	private void addObjects() {		
		cp = jfrmProtocols.getContentPane();
		cp.setLayout(null);
//		cp.add(jcbxProtocolList);
		cp.add(jlblAverage);
		cp.add(jtxtAverage);
		
		cp.add(jlblBonusGiven);
		cp.add(jtxtBonusGiven);
		
		cp.add(jlblBonusSpent);
		cp.add(jtxtBonusSpent);		
		
	}
}
